<?php

class Application_Model_ProductBrowseMapper
{
	protected $_dbTable;
	
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	
	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_ProductBrowse');
		}
		return $this->_dbTable;
	}
	
	public function insertAll(Application_Model_ProductBrowse $obj)
    {
        $data = array(
            'product_buy_id' => $obj->getProduct_buy_id(),
        	'amount'   => $obj->getAmount(),
        	'creation_time' => $obj->getCreation_time()
        );
        $this->getDbTable()->insert($data);
    }
    
	public function updateAll(Application_Model_ProductBrowse $obj)
    {
    	$db = $this->getDbTable();
    	
         $data = array(
            'product_buy_id' => $obj->getProduct_buy_id(),
        	'amount'   => $obj->getAmount(),
        	'creation_time' => $obj->getCreation_time()
        );
        $db->update($data,array('product_browse_id = ?' => $obj->getProduct_browse_id()));
    }
    
	public function findByResult($select){
		$db = $this->getDbTable();
    	$resultSet = $db->fetchAll($select);
        $entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_ProductBrowse();
			$entry->setProduct_browse_id($row->product_browse_id)
				->setProduct_buy_id($row->product_buy_id)
				->setAmount($row->amount)
				->setCreation_time($row->creation_time);
			$entries[] = $entry;
		}
		return $entries;
    }
    
	public function findAll()
    {
    	$db = $this->getDbTable();
        $select = $db->select()->from('product_browse');
    	return $this->findByResult($select);
    }

	public function findByProduct_buy_id($product_buy_id)
    {
    	if($product_buy_id != null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('product_browse')
	             ->where('product_buy_id = ?', $product_buy_id);
	    	
	    	return $this->findByResult($select);
    	}
    }
    
    public function findByPbidCreationTime($product_buy_id, $creation_time){
    	if($product_buy_id != null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('product_browse')
	        	->where('product_buy_id = ?', $product_buy_id)
	            ->where('creation_time = ?', $creation_time);
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function findAmountByProduct_buy_id($product_buy_id)
    {
    	if($product_buy_id!=null){
	    	$db = $this->getDbTable();
	    	
	    	$select = $db->select()
	    	->from(array('a'=>'product_browse'), array('product_buy_id', 'amount' => 'sum(amount)'))
			->where('product_buy_id = ?', $product_buy_id)
			->group('product_buy_id');
			
			$select->setIntegrityCheck(false); 
	    	
	    	return $this->findByResult($select);
    	}
    }
}

